import config from "../../config/config";
import Client from "../../config/client";
Page({
  data: {
    onLoad: false,
    Client: null,
    gameMessage: JSON.parse(JSON.stringify(config.gameMessage)), // 游戏数据
    pageTopHeight: wx.getWindowInfo().statusBarHeight, // 顶部高度
    gameStateDescribe: config.gameStateDescribe,
    // --------------- 客户界面-----------------
    gameStateCard: 0, 
    // 弹窗状态
    modalCole: 0,
    // 被选择的玩家
    selectObj: {},
    // 女巫用户点击的是毒还是救
    witchtype: 0,  // 1毒 2救
    // 自己的游戏数据
    ownGameMessage: {},
    // 历史数据
    historyMessage: [], // d天 有o就是投票 u被淘汰的人
    // 可以被投票的玩家
    canGameVote:[],
    //0 禁止操作  1.可操作 2.被操作  3.弃权
    operated: 0,
    // 淘汰的人
    knockOut:[],
    // 已选角色 [{name: numerber: }]
    roleAndCount:[],
    // 倒计时时间
    progressTime: 3,
    // 倒计时百分比
    progressPercent: 100,
    // 是否是分享状态
    share:false,
    count:0, userCount:0, lineCount:0, liveCount:0,
   },

  onLoad(option) {
    const ip = option.ip;
    const back = option.back;
    const user = wx.getStorageSync("userInfo");
    console.log(user,'user',!user.userPicture || !user.userName);
    if(!user.userPicture || !user.userName){
      this.setData({
        onLoad: true,
      });
      getApp().globalData.showToastTitle = "请完善信息重新加入";
      const pages = getCurrentPages();
      const routeStackLength = pages.length;
      routeStackLength > 1 ? wx.navigateBack({ delta: 2}) : wx.redirectTo({ url: '/pages/home/home' })
      return;
    }
    if (ip) {
      if (this.data.Client) {
        this.data.Client.destroy();
      }
      this.setData({
        onLoad: true,
        Client: new Client(this),
      });
      this.data.Client.gameMessage.gameIp = ip;
      // 尝试加入房间
      this.data.Client.tryJoinRoom(ip,back);
      // 手机常亮
      wx.setKeepScreenOn({ keepScreenOn: true });
    } else {
      this.setData({
        onLoad: true,
      });
      getApp().globalData.showToastTitle = "加入失败";
      const pages = getCurrentPages();
      const routeStackLength = pages.length;
      routeStackLength > 1 ? wx.navigateBack({ delta: 2}) : wx.redirectTo({ url: '/pages/home/home' })
      return;
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp();
    const _this = this;
    
    if(this.data.share){
      this.setData({ share: false })
      return;
    }
    // 在游戏房间内冒泡
    if (!this.data.onLoad) {
      this.data.Client && this.data.Client.clientTryConnect();
    }
    this.setData({ onLoad: false });
  },
  /**
   * 生命周期函数--监听页面隐藏 客户端离开房间
   */
  onHide() {
    // 玩家离线切出
    if( this.data.Client && !this.data.share){
      this.data.Client.onLeave();
    }
    console.log("隐藏");
  },
  // 销毁
  onUnload() {
    if (this.data.Client) {
      //告诉法官  我直接退出了
      this.data.Client.exitRoom();
      setTimeout(() => {
        this.destroy();
      }, 2000);
    }
    // 手机常亮
    wx.setKeepScreenOn({ keepScreenOn: false });
  },
  // 销毁
  destroy() {
    this.data.Client && this.data.Client.destroy();
    this.setData({ Client: null });
  },
  // ----------弹窗 start---------
  // 关闭弹窗
  close() {
    this.setData({ modalCole: 0 });
    console.log("========关闭=======",this.data.gameStateCard);
  },
  // 退出房间
  goPage() {
    this.setData({ modalCole: 7 });
  },
  // 网络断开退出
  networkOut() {
    this.data.Client.networkOut();
  },
  // 网络断开重连
  networkTry() {
    this.data.Client.networkTry();
  },
  // 查看图标说明
  seeIcon(e){
    wx.showToast({
      title: e.currentTarget.dataset.mes,
      icon: "none",
      duration: 3000,
    });
  },
  // 放弃营救
  pluckUp(){
    if(this.data.gameMessage.witchKill){
      this.setData({ witchtype: 1 });
      // 更新击杀列表
      this.data.Client.setCanGameVote()
    } else {
      // 走放弃
      this.setData({ operated: 3 });
      this.giveUpOk()
    }
  },
  // 营救
  pluck(){
    this.data.gameMessage.players.forEach((item) => {
      if (item.ip === this.data.gameMessage.WKU) {
        this.setData({ selectObj: item, witchtype: 2 });
      }
    });
    this.data.Client.selectOk();
  },
    // 查看自己身份
  seeMeStyle(e){
    if(e.currentTarget.dataset.type === '1'){
      this.setData({ modalCole: 17 });
    }
  },
  // 角色选择 操作
  selectGameUser(element){
    const selectObj = element.currentTarget.dataset.item;
    console.log(selectObj,'selectObj');
    if(this.data.witchtype !== 2){
      this.setData({ selectObj });
    }
  },
  // 放弃操作
  giveUp() {
    this.setData({ modalCole: 5 });
  },
  // 放弃操作
  giveUpOk() {
    if(this.data.gameStateCard === 5){
      this.giveUpHunterOk()
    }else{
      this.data.Client.giveUpOk();
    }
  },
    // 猎人放弃
  giveUpHunterOk() {
    this.data.Client.giveUpHunterOk()
  },
  // 确认退出
  exitRoomOk() {
    this.data.Client.exitRoomOk();
  },
  // 查看关闭
  seeClose() {
    this.setData({ modalCole: 0, operated: 0, gameStateCard: 0, selectObj:{} });
  },
  // 关闭技能弹窗
  closeGameState() {
    this.data.Client.closeGameState();
  },
  // 打开投票弹窗
  goVote() {
    this.data.Client.gameMessage.gameState = 105
    this.data.Client.gameMessage = this.data.Client.gameMessage
    this.data.Client.livePermitOperated()
    this.data.Client.setCanGameVote()
    this.setData({ modalCole: 0, operated: 1, gameStateCard: 105 });
  },
  // 去带人
  goHunter() {
    this.setData({ modalCole: 0, operated: 1, gameStateCard: 5 });
    this.data.Client.gameMessage.gameState = 5
  },
  // ----------弹窗 end---------
  // 更新用户消息
  upDataUserInfo() {
    if (this.data.Client && this.data.gameMessage.roomId) {
      this.data.Client.upDataUserInfo();
    }
  },
  peepatNo() {
    this.setData({ peepatState: false });
  },
  peepatOk() {
    this.setData({ peepatState: true });
  },
  // 网络断开退出
  networkOut() {
    this.data.Client.networkOut();
  },
  // 网络断开重连
  networkTry() {
    this.data.Client.networkTry();
  },
  // 查看角色
  seePart() {
    wx.showToast({
      title: this.data.Client.ownGameMessage.name,
      icon: "none",
      duration: 3000,
    });
  },
  // 更新用户信息
  setUserInfo() {
    this.data.Client.upDataUserInfo();
  },

  // 确认操作
  selectOk() {
    this.data.Client.selectOk();
  },
  // 分享转发
  onShareAppMessage() {
    this.setData({ share: true })
    // const promise = new Promise((resolve) => {
    //   setTimeout(() => {
    //     resolve({
    //       title: "速来抓狼，就差你了",
    //     });
    //   }, 2000);
    // });
    return {
      title: "速来抓狼，就差你了",
      path: `/pages/cRoom/cRoom?ip=${this.data.gameMessage.gameIp || ""}`,
      // promise,
    };
  },
});
